/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**

 @author Marie-Claude
 */
public class Message {

  private User sendingUser;
  private int messageID;
  private int senderID;
  private int recipientID;
  private String messageText;
  private Message inReplyToMsg;
  private String subject;
  private String recipientUsername;
  private Date date;
  private final String SUBJECT_PLACEHOLDER = "No subject";

  public int getMessageID() {
    return messageID;
  }

  public void setMessageID(int messageID) {
    this.messageID = messageID;
  }

  public int getSenderID() {
    return senderID;
  }

  public void setSenderID(int senderID) {
    this.senderID = senderID;
  }

  public int getRecipientID() {
    return recipientID;
  }

  public void setRecipientID(int recipientID) {
    this.recipientID = recipientID;
  }

  public Message getInReplyToMsg() {
    return inReplyToMsg;
  }

  public void setInReplyToMsg(Message inReplyToMsg) {
    this.inReplyToMsg = inReplyToMsg;
  }

  public User getSendingUser() {
    return sendingUser;
  }

  public void setSendingUser(User sendingUser) {
    this.sendingUser = sendingUser;
  }

  public String getMessageText() {
    return messageText;
  }

  public void setMessageText(String messageText) {
    this.messageText = messageText;
  }

  public Message getSubMessage() {
    return inReplyToMsg;
  }

  public void setSubMessage(Message msg) {
    inReplyToMsg = msg;
  }

  public String getSubject() {
    return (subject == null ? SUBJECT_PLACEHOLDER : subject);
  }

  public void setSubject(String subject) {
    this.subject = (subject == null ? SUBJECT_PLACEHOLDER : subject);
  }

  public String getRecipientUsername() {
    return recipientUsername;
  }

  public void setRecipientUsername(String recipientUsername) {
    this.recipientUsername = recipientUsername;
  }

  public String getDate() {
    //TODO rewrite to less ugly
    if (date == null) {
      return "";
    }
    String dateString;
    DateFormat df;
    if (dateIsToday()) {
      df = new SimpleDateFormat("HH:mm");
    } else if (dateIsThisYear()) {
      df = new SimpleDateFormat("MMMM dd");
    } else {
      df = new SimpleDateFormat("MM/dd/yyyy");
    }
    dateString = df.format(date);
    return dateString;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  private boolean dateIsToday() {
    Date today = new Date();
    boolean isToday = false;
    if (date.getDay() == today.getDay()
            && date.getMonth() == today.getMonth()
            && date.getYear() == today.getYear()) {
      isToday = true;
    }
    return isToday;
  }

  private boolean dateIsThisYear() {
    boolean isThisYear = false;
    Date today = new Date();
    if (date.getYear() == today.getYear()) {
      isThisYear = true;
    }
    return isThisYear;
  }
}
